package org.zoomdev.zoom.web.router;

import org.zoomdev.zoom.web.action.impl.ServletActionHandler;

import javax.servlet.http.HttpServlet;

public final class RouterBuilder {

    private final Router router;

    private final RouterParameterRule rule;


    protected RouterBuilder(Router router) {
        this.router = router;
        this.rule = new RouterParameterRule();
    }

    public static RouterBuilder newBuilder(Router router) {
        return new RouterBuilder(router);
    }

    public RouterBuilder add(String key, HttpServlet servlet, String... methods) {
        String fullKey = key.startsWith("/") ? key : ("/" + key);
        router.add(new ServletActionHandler(fullKey, rule.getParameterNames(fullKey), methods, servlet));
        return this;
    }
}
